import { WORD_REGEX } from "./internal/defines";

/**
 * Convert a string to underline splitting words in lowercase, with keeping all
 * leading underlines and continues underlines.
 *
 * example:
 * ```
 * _name_styles___is_a_good_library
 * ```
 *
 * @param {string} s source string
 * @return {string} converted result with underline splitting words in lowercase.
 */
export function underscore(s: string): string {
    return s
        .replace(/[-\s]/g, "_")
        .replace(WORD_REGEX, function (ch) { return "_" + ch; })
        .replace(/(^|_)_(?=[A-Z])/g, "$1")
        .toLowerCase();
}
